﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RN.ERP.DAL;
using RN.ERP.Entity.ZBDLRY;
using RN.ERP.DAL.Context;
using Fly.Log;
using System.Configuration;

using User_Log = Fly.Log.User_Log;
using User_Log_Data = Fly.Log.User_Log_Data;

namespace RN.ERP.BLL.ZBDLRY.Utilities
{
    public class LogAndFlowHelper
    {
        public string AppId
        {
            get
            {
                string appId = ConfigurationManager.AppSettings["AppId"];
                if (!string.IsNullOrEmpty(appId))
                {
                    return appId;
                }
                return "98";
            }
        }
        ZBDL_Context dbContext = new ZBDL_Context();

        /// <summary>
        /// 企业、人员增加操作时同时向tb_flowcode表中插入一条数据
        /// </summary>
        /// <param name="flowcode"></param>
        /// <returns></returns>
        public bool AddFlowcode(Entity.ZBDLRY.Tb_FlowCode_Zbdl flowcode)
        {
            if (flowcode != null)
            {
                int appid = 0;
                int.TryParse(System.Configuration.ConfigurationManager.AppSettings["AppID"], out appid);
                flowcode.AppID = appid;
                dbContext.FlowCodes.Add(flowcode);
                if (dbContext.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 返回是否上报
        /// </summary>
        /// <param name="objectId">Guid</param>
        /// <param name="areaCode">地区号</param>
        /// <param name="codeType">功能码</param>
        /// <returns>false：未上报或退回，true：上报或审核</returns>
        public bool CheckFlowCode(string objectId, string areaCode, int codeType)
        {
            string flowCode = string.Empty;
            areaCode = areaCode.Length > 2 ? areaCode.Substring(0, 2) : areaCode;
            if ("06" == areaCode)
            {
                int appid = 0;
                int.TryParse(System.Configuration.ConfigurationManager.AppSettings["AppID"], out appid);
                flowCode = dbContext.FlowCodes.Where(f => f.ObjectId == objectId && f.AreaCode == areaCode && f.CodeType == codeType
                    && f.IsDel == false && f.AppID == appid).Select(f => f.FlowCode).FirstOrDefault();
            }
            else
                flowCode = dbContext.FlowCodes.Where(f => f.ObjectId == objectId && f.AreaCode == areaCode && f.CodeType == codeType && f.IsDel == false).Select(f => f.FlowCode).FirstOrDefault();
            if (string.IsNullOrEmpty(flowCode) || flowCode.Trim() == "0")////未上报
            {
                return false;
            }
            if (flowCode.Trim().IndexOf("1,") > 1)////退回
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="content"></param>
        /// <param name="objectid"></param>
        /// <param name="user"></param>
        public void InsertUserLog(string content, string objectid, string functionid, string functiontype)
        {
            UserLogManager.Log(new User_Log()
            {
                Content = content,
                ObjectId = objectid,
                FunctionId = functionid,
                FunctionType = functiontype,
                AppId = AppId
            });
        }

        /// <summary>
        /// 插入详细日志记录
        /// </summary>
        /// <param name="content"></param>
        /// <param name="objectid"></param>
        /// <param name="user"></param>
        public void InsertDetailUserLog(string content, string objectid, string functionid, string functiontype, User_Log log)
        {
            UserLogManager.Log(new User_Log()
            {
                Content = content,
                ObjectId = objectid,
                FunctionId = functionid,
                FunctionType = functiontype,
                AppId = AppId,
                IsrtDate = log.IsrtDate,
                IsrtUserArea = log.IsrtUserArea,
                IsrtUserDept = log.IsrtUserDept,
                IsrtUser = log.IsrtUser,
                IsrtUserName = log.IsrtUserName,
                IsrtUserRole = log.IsrtUserRole,
                Notice = log.Notice
            });
        }

        public void InsertFlowLog(string content, string objectid, string functionid, string functiontype, string notice, object obj)
        {
            UserLogManager.Log(new User_Log()
            {

                Content = content,
                ObjectId = objectid,
                FunctionId = functionid,
                FunctionType = functiontype,
                Notice = notice,
                AppId = AppId
            }, obj);

        }
        public IList<User_Log> GetUserLog(string objectid, string IsrtUser, string areaCode)
        {
            return null;//UserLogManager.GetLog(AppId, null, null, objectid, IsrtUser,null,null,areaCode).OrderByDescending(l=>l.IsrtDate).ToList();
        }
    }
}
